//
//  SoundEffectsPrefsController.m
//  Display Plugins
//
//  Created by Matthew Walton on 11/09/2004.
//  Copyright 2004-2005 The Growl Project. All rights reserved.
//

#import "SoundEffectsPrefsController.h"
#import "SoundEffectsDefines.h"
#import "GrowlDefinesInternal.h"
#import "NSSound+SoundList.h"
#import "GrowlDefines.h"
#import "GrowlApplicationBridge.h"

@implementation SoundEffectsPrefsController

- (id) init
{
	self = [super init];
	
	rules = nil;
	
	return self;
}

- (NSString *) mainNibName 
{
	return @"SoundEffectsPrefs";
}

+ (void) loadColorWell:(NSColorWell *)colorWell fromKey:(NSString *)key defaultColor:(NSColor *)defaultColor 
{
	NSData *data = nil;
	NSColor *color;
	READ_GROWL_PREF_VALUE(key, SoundEffectsPrefDomain, NSData *, &data);
	if (data && [data isKindOfClass:[NSData class]]) {
		color = [NSUnarchiver unarchiveObjectWithData:data];
	} else {
		color = defaultColor;
	}
	[colorWell setColor:color];
	[data release];
}

- (void) mainViewDidLoad 
{
	[slider_opacity setAltIncrementValue:0.05];

	// priority colour settings
	NSColor *defaultColor = [NSColor colorWithCalibratedWhite:0.1f alpha:1.0f];

	[SoundEffectsPrefsController loadColorWell:color_veryLow fromKey:SoundEffectsVeryLowColor defaultColor:defaultColor];
	[SoundEffectsPrefsController loadColorWell:color_moderate fromKey:SoundEffectsModerateColor defaultColor:defaultColor];
	[SoundEffectsPrefsController loadColorWell:color_normal fromKey:SoundEffectsNormalColor defaultColor:defaultColor];
	[SoundEffectsPrefsController loadColorWell:color_high fromKey:SoundEffectsHighColor defaultColor:defaultColor];
	[SoundEffectsPrefsController loadColorWell:color_emergency fromKey:SoundEffectsEmergencyColor defaultColor:defaultColor];

	defaultColor = [NSColor whiteColor];

	[SoundEffectsPrefsController loadColorWell:text_veryLow fromKey:SoundEffectsVeryLowTextColor defaultColor:defaultColor];
	[SoundEffectsPrefsController loadColorWell:text_moderate fromKey:SoundEffectsModerateTextColor defaultColor:defaultColor];
	[SoundEffectsPrefsController loadColorWell:text_normal fromKey:SoundEffectsNormalTextColor defaultColor:defaultColor];
	[SoundEffectsPrefsController loadColorWell:text_high fromKey:SoundEffectsHighTextColor defaultColor:defaultColor];
	[SoundEffectsPrefsController loadColorWell:text_emergency fromKey:SoundEffectsEmergencyTextColor defaultColor:defaultColor];

	[[[[rulesTableView tableColumns] objectAtIndex:0] dataCell] setFont:[NSFont systemFontOfSize:11]];
}

- (float) opacity 
{
	float value = SoundEffectsAlphaPrefDefault;
	READ_GROWL_PREF_FLOAT(SoundEffectsAlphaPref, SoundEffectsPrefDomain, &value);
	return value;
}

- (void) setOpacity:(float)value 
{
	WRITE_GROWL_PREF_FLOAT(SoundEffectsAlphaPref, value, SoundEffectsPrefDomain);
	UPDATE_GROWL_PREFS();
}

- (float) duration 
{
	float value = SoundEffectsDurationPrefDefault;
	READ_GROWL_PREF_FLOAT(SoundEffectsDurationPref, SoundEffectsPrefDomain, &value);
	return value;
}

- (void) setDuration:(float)value 
{
	WRITE_GROWL_PREF_FLOAT(SoundEffectsDurationPref, value, SoundEffectsPrefDomain);
	UPDATE_GROWL_PREFS();
}

- (IBAction) colorChanged:(id)sender 
{
	NSString *key;
	switch ([sender tag]) {
		case -2:
			key = SoundEffectsVeryLowColor;
			break;
		case -1:
			key = SoundEffectsModerateColor;
			break;
		case 1:
			key = SoundEffectsHighColor;
			break;
		case 2:
			key = SoundEffectsEmergencyColor;
			break;
		case 0:
		default:
			key = SoundEffectsNormalColor;
			break;
	}

	NSData *theData = [NSArchiver archivedDataWithRootObject:[sender color]];
	WRITE_GROWL_PREF_VALUE(key, theData, SoundEffectsPrefDomain);
	UPDATE_GROWL_PREFS();
}

- (IBAction) textColorChanged:(id)sender 
{
	NSString *key;
	switch ([sender tag]) {
		case -2:
			key = SoundEffectsVeryLowTextColor;
			break;
		case -1:
			key = SoundEffectsModerateTextColor;
			break;
		case 1:
			key = SoundEffectsHighTextColor;
			break;
		case 2:
			key = SoundEffectsEmergencyTextColor;
			break;
		case 0:
		default:
			key = SoundEffectsNormalTextColor;
			break;
	}

	NSData *theData = [NSArchiver archivedDataWithRootObject:[sender color]];
	WRITE_GROWL_PREF_VALUE(key, theData, SoundEffectsPrefDomain);
	UPDATE_GROWL_PREFS();
}

- (BOOL) isFloatingIcon 
{
	BOOL value = SoundEffectsFloatIconPrefDefault;
	READ_GROWL_PREF_BOOL(SoundEffectsFloatIconPref, SoundEffectsPrefDomain, &value);
	return value;
}

- (void) setFloatingIcon:(BOOL)value 
{
	WRITE_GROWL_PREF_BOOL(SoundEffectsFloatIconPref, value, SoundEffectsPrefDomain);
	UPDATE_GROWL_PREFS();
}

- (BOOL) isLimit 
{
	BOOL value = SoundEffectsLimitPrefDefault;
	READ_GROWL_PREF_BOOL(SoundEffectsLimitPref, SoundEffectsPrefDomain, &value);
	return value;
}

- (void) setLimit:(BOOL)value 
{
	WRITE_GROWL_PREF_BOOL(SoundEffectsLimitPref, value, SoundEffectsPrefDomain);
	UPDATE_GROWL_PREFS();
}

- (int) numberOfItemsInComboBox:(NSComboBox *)aComboBox 
{
#pragma unused(aComboBox)
	return [[NSScreen screens] count];
}

- (id) comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(int)idx 
{
#pragma unused(aComboBox)
	return [NSNumber numberWithInt:idx];
}

- (int) screen 
{
	int value = 0;
	READ_GROWL_PREF_INT(SoundEffectsScreenPref, SoundEffectsPrefDomain, &value);
	return value;
}

- (void) setScreen:(int)value 
{
	WRITE_GROWL_PREF_INT(SoundEffectsScreenPref, value, SoundEffectsPrefDomain);
	UPDATE_GROWL_PREFS();
}

- (int) size 
{
	int value = 0;
	READ_GROWL_PREF_INT(SoundEffectsSizePref, SoundEffectsPrefDomain, &value);
	return value;
}

- (void) setSize:(int)value {
	WRITE_GROWL_PREF_INT(SoundEffectsSizePref, value, SoundEffectsPrefDomain);
	UPDATE_GROWL_PREFS();
}

- (NSArray *) getSoundList 
{
	NSMutableArray * array = [[NSMutableArray alloc] initWithArray:[NSSound availableSounds]];
	[array addObject:@"No Sound"];
	return array;
}

- (NSString *) getSoundName
{
	NSString * value = nil;
	READ_GROWL_PREF_VALUE(SoundEffectsSoundNamePref, SoundEffectsPrefDomain, NSString *, &value);
		
	return value;
}

- (void) setSoundName:(NSString *) value
{
	WRITE_GROWL_PREF_VALUE(SoundEffectsSoundNamePref, value, SoundEffectsPrefDomain);
	UPDATE_GROWL_PREFS();
}

- (NSMutableArray *) getRules
{
	if (rules == nil)
	{
		NSArray * prefRules = nil;
		
		READ_GROWL_PREF_VALUE (SoundEffectsRulesPref, SoundEffectsPrefDomain, NSArray *, &prefRules);
	
		if (prefRules == nil)
			rules = [[NSMutableArray alloc] init];
		else
		{
			rules = [[NSMutableArray alloc] init];
		
			unsigned i = 0;
			for (i = 0; i < [prefRules count]; i++)
			{
				NSDictionary * item = [prefRules objectAtIndex:i];
				[rules addObject:[NSMutableDictionary dictionaryWithDictionary:item]];
			}
		}
	}
	
	return rules;
}

- (void) setRules:(NSMutableArray *) value
{
	rules = [[NSMutableArray alloc] initWithArray:value];
	WRITE_GROWL_PREF_VALUE(SoundEffectsRulesPref, value, SoundEffectsPrefDomain);
	UPDATE_GROWL_PREFS();
}

- (void) dealloc 
{
	[self setRules:rules];
	
	[rules release];
	
	[super dealloc];
}

- (IBAction) saveRules:(id) sender
{
	[self setRules:[self getRules]];
}

@end
